package com.zms.maze;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zms.maze.MummyConst.Direction;

//This class will handle the transactions from one state to other.
//It provides the ways to access query state and directon.
public class Transaction {

	//lastmaze+dir=maze
	private Hashtable<Action,Maze> transaction=new Hashtable<Action,Maze>();
	
	//maze= lastmaze+dir
	public void put(Maze maze, Direction dir, Maze lastMaze)
	{
		transaction.put(new Action(lastMaze.clone(),dir),maze.clone());
//		String log=lastMaze.explorer.getPosString()+"-"+lastMaze.mummies.get(0).getPosString()+dir.toString()+"->"+maze.explorer.getPosString()+"-"+maze.mummies.get(0).getPosString();
//		Utilities.getInstance().log(log);
	}
	//you use action as key to query value,cloning it isn't essential.
	public Maze getNextMaze(Maze maze, Direction dir)
	{
		return transaction.get(new Action(maze,dir));
	}
	
	public List<Action> getPreviousMaze(Maze maze)
	{
		List<Action> preMazes=new ArrayList<Action>();
		for( Entry<Action, Maze>  entry:this.transaction.entrySet())
		{
			if (entry.getValue().equals(maze))
				preMazes.add(entry.getKey().clone());
		}
		return preMazes;
	}
	
	public boolean contains(Maze maze, Direction dir)
	{
		if (transaction.containsKey(new Action(maze,dir)))
			return true;
		return false;
	}
	public Hashtable<Action, Maze> getTransaction()
	{
		return this.transaction;
	}
	
}
